from services import *


def insert_user_from_console():
    user_id = int(input("请输入用户id:"))
    user_name = input("请输入用户名：")
    user = User(user_id, user_name)
    insert_user(user)
    print("用户插入成功")


def insert_book_from_console():
    book_id = int(input("请输入图书id:"))
    book_name = input("请输入图书名：")
    book = Book(book_id, book_name)
    insert_book(book)
    print("图书插入成功")


def print_user_list():
    print('-' * 40)
    print("用户列表：")
    for k, v in get_user_list().items():
        print(k, v)
    print('-' * 40)


def print_book_list():
    print('-' * 40)
    print("图书列表：")
    for k, v in get_book_list().items():
        print(k, v)
    print('-' * 40)


def print_record_list():
    print('-' * 40)
    print("借书记录：")
    user_list = get_user_list()
    book_list = get_book_list()
    for u_id, b_id in list(get_record_list()):
        print(f"{u_id} {user_list.get(u_id)}-{b_id} {book_list.get(b_id)}")
    print('-' * 40)


def borrow_book_from_console():
    u_id = int(input("读者id"))
    assert u_id in get_user_list(), "读者id不存在"
    b_id = int(input("借阅的图书id"))
    assert b_id in get_book_list(), "图书id不存在"
    record = Record(u_id, b_id)
    insert_record(record)


if __name__ == '__main__':
    while True:
        try:
            print("请输入你要进行的操作：")
            print("1: 添加读者  2: 获取读者列表  3: 删除读者")
            print("4: 增加图书库存  5: 获取图书列表 6: 删除图书")
            print("7: 借书  8: 还书  9:获取图书借阅记录")
            i = int(input())
            if i == 1:
                insert_user_from_console()
            elif i == 2:
                print_user_list()
            elif i == 4:
                insert_book_from_console()
            elif i == 5:
                print_book_list()
            elif i == 7:
                borrow_book_from_console()
            elif i == 9:
                print_record_list()
            else:
                print("暂不支持的操作")
        except Exception as e:
            print(e)
